import db from "../utils/db"
import { isEmptyStr, isEmptyObject} from "../utils/util"

export function addMeasureData (data) {
  db.measuredata.put(data);
}

export function countMeasureData(condition) {
  let flag = false;
  let queryCondition = {};
  if (!isEmptyObject(condition) && !isEmptyStr(condition.sample)) {
    queryCondition["sample"] = condition.sample;
    flag = true;
  }
  if (!isEmptyObject(condition) && !isEmptyStr(condition.username)) {
    queryCondition["username"] = condition.username;
    flag = true;
  }
  if (!isEmptyObject(condition) && !isEmptyStr(condition.createOn)) {
    queryCondition["createOn"] = condition.createOn;
    flag = true;
  }
  if (flag) {
    return db.measuredata.where(queryCondition).count();
  } else {
    return db.measuredata.count();
  }
}

export function queryMeasureDataList(condition) {

  let flag = false;
  let queryCondition = {};
  if (!isEmptyObject(condition) && !isEmptyStr(condition.sample)) {
    queryCondition["sample"] = condition.sample;
    flag = true;
  }
  if (!isEmptyObject(condition) && !isEmptyStr(condition.username)) {
    queryCondition["username"] = condition.username;
    flag = true;
  }

  if (!isEmptyObject(condition) && !isEmptyStr(condition.createOn)) {
    queryCondition["createOn"] = condition.createOn;
    flag = true;
  }
  if (flag) {
    return db.measuredata.where(queryCondition).reverse().offset(condition.start).limit(condition.size).toArray();
  } else {
    return db.measuredata.reverse().offset(condition.start).limit(condition.size).toArray();
  }

}

export function deleteMeasureData(id) {
  db.measuredata.delete(id)
}
